﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using FinalPointSet.Model.Asset;
using FinalPointSet.Model.Constant;
using FinalPointSet.Model.Constant.Message;
using FinalPointSet.Model.Constants;
using FinalPointSet.Model.Manager;
using FinalPointSet.Model.Model.Knowledge;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;

namespace FinalPointSet.ViewModel.Page.Knowledge
{
    public partial class LanguageSettingPageViewModel : ObservableObject
    {
        /// <summary>
        /// 语言集合
        /// </summary>
        [ObservableProperty]
        private ObservableCollection<Language> _Languages = [];
        /// <summary>
        /// 代码高亮类型集合
        /// </summary>
        [ObservableProperty]
        private List<HighLight> _HighLights = [];
        /// <summary>
        /// 当前选中的语言
        /// </summary>
        [ObservableProperty]
        private Language _SelectedItem = new();
        /// <summary>
        /// 选择图标弹窗是否可见
        /// </summary>
        [ObservableProperty]
        private bool _IsIconPickerVisible = false;
        /// <summary>
        /// 图标路径集合
        /// </summary>
        [ObservableProperty]
        private List<TextPair> _LogoPaths = [];
        public LanguageSettingPageViewModel()
        {
            InitResource();
        }

        public void InitResource()
        {
            Languages = new(AssetManager.Instance.GetJsonObjectList<Language>().OrderBy(lang => lang.Sequence));
            HighLights = AssetManager.Instance.GetJsonObjectList<HighLight>();
            LogoPaths = AssetManager.Instance.GetTextList(Path.LanguageLogo);
        }

        /// <summary>
        /// 新增
        /// </summary>
        [RelayCommand]
        private void Add()
        {
            Debug.Print("KnowledgePoint.SettingPageViewModel.Add(), 新增语言");
            int maxSeq = (Languages.Count > 0) ? Languages.Max(lang => lang.Sequence) + 1 : 1;
            string name = $"语言{maxSeq}";
            Language lang = new()
            {
                Sequence = maxSeq,
                Name = name,
                LogoPath = ".Assets/Images/Language/Other.svg",
                HighLight = new HighLight("Text")
            };
            Languages.Add(lang);
            MessageManager.Broadcast(MsgType.GrowMsg, new GrowMsgArg(WindowType.MainView, GrowlMsgType.Success, $"新增语言【{lang.Name}】成功!"));
        }

        /// <summary>
        /// 移除语言
        /// </summary>
        [RelayCommand]
        private void Remove(Language lang)
        {
            Debug.Print($"KnowledgePoint.SettingPageViewModel.Remove(), 移除语言:{lang.Name}");
            if (Languages != null && Languages.Contains(lang))
            {
                Languages.Remove(lang);
                Debug.Print($"KnowledgePoint.SettingPageViewModel.Remove(), 移除语言【{lang.Name}】成功!");
                MessageManager.Broadcast(MsgType.GrowMsg, new GrowMsgArg(WindowType.MainView, GrowlMsgType.Success, $"移除语言【{lang.Name}】成功!"));
            }
            else
            {
                Debug.Print($"KnowledgePoint.SettingPageViewModel.Remove(), 移除语言【{lang.Name}】失败!");
                MessageManager.Broadcast(MsgType.GrowMsg, new GrowMsgArg(WindowType.MainView, GrowlMsgType.Error, $"移除语言【{lang.Name}】失败!"));
            }
        }

        /// <summary>
        /// 上移语言
        /// </summary>
        [RelayCommand]
        private void MoveUp(Language lang)
        {
            if (lang == null) return;
            int index = Languages.IndexOf(lang);
            if (index <= 0)
            {
                Debug.Print($"KnowledgePoint.SettingPageViewModel.MoveUp(), 当前语言已位于最顶部, 无法移动");
                MessageManager.Broadcast(MsgType.GrowMsg, new GrowMsgArg(WindowType.MainView, GrowlMsgType.Warning, "当前语言已位于最顶部, 无法移动"));
                return;
            }
            //交换排序索引
            (Languages[index].Sequence, Languages[index - 1].Sequence) = (Languages[index - 1].Sequence, Languages[index].Sequence);
            Languages = new(Languages.ToList().OrderBy(lang => lang.Sequence));
            Debug.Print($"KnowledgePoint.SettingPageViewModel.MoveUp(), 上移语言:{lang.Name}");
        }

        /// <summary>
        /// 下移语言
        /// </summary>
        [RelayCommand]
        private void MoveDown(Language lang)
        {
            if (lang == null) return;
            int index = Languages.IndexOf(lang);
            if (index >= Languages.Count - 1)
            {
                Debug.Print($"KnowledgePoint.SettingPageViewModel.MoveDown(), 当前语言已位于最底部, 无法移动");
                MessageManager.Broadcast(MsgType.GrowMsg, new GrowMsgArg(WindowType.MainView, GrowlMsgType.Warning, "当前语言已位于最底部, 无法移动"));
                return;
            }
            //交换排序索引
            (Languages[index].Sequence, Languages[index + 1].Sequence) = (Languages[index + 1].Sequence, Languages[index].Sequence);
            Languages = new(Languages.ToList().OrderBy(lang => lang.Sequence));
            Debug.Print($"KnowledgePoint.SettingPageViewModel.MoveDown(), 下移语言:{lang.Name}");
        }
        /// <summary>
        /// 保存
        /// </summary>
        [RelayCommand]
        private void Save()
        {
            if (Languages == null) return;
            //保存数据
            AssetManager.Instance.UpdateJsonObjectList(Languages.ToList());
            AssetManager.Instance.SaveJsonObjectList<Language>(new LanguageConverter());
            MessageManager.Broadcast(MsgType.GrowMsg, new GrowMsgArg(WindowType.MainView, GrowlMsgType.Success, $"保存语言成功"));
        }

        [RelayCommand]
        private void ComboBoxSelectionChanged(Language lang)
        {
            Debug.Print($"KnowledgePoint.SettingPageViewModel.ComboBoxSelectionChanged(), 元素={lang.Name}, 高亮={lang.HighLight.Name}");
            if (lang == null) return;
            HighLight highLight = HighLights.FirstOrDefault(hl => hl.Name.Equals(lang.HighLight.Name), new HighLight(lang.Name));
            lang.HighLight.HighlightingDefinition = highLight.HighlightingDefinition;
        }
    }
}
